#include "ScorpSpear.h"
#include "Player.h"
#include "../SGD Wrappers/DestroyEntityMessage.h"

CScorpSpear::CScorpSpear(Entity* owner)
{
	m_pOwner = owner;
}


CScorpSpear::~CScorpSpear()
{
}

void CScorpSpear::HandleCollision(const Entity* pOther) /*override*/
{

	// If you collide with a Cannyble or the Ball, pull them towards your owner
	if ((pOther->GetType() == ENT_CANNYBLE || pOther->GetType() == ENT_BALL) && m_bHitEnemy == false)
	{
		m_bHitEnemy = true;

		Entity* tempOther = const_cast<Entity*>(pOther);

		tempOther->SetSmashVel(m_pOwner->GetPosition() - m_ptPosition);

		int stealChance = rand() % 100;

		if (m_nLevel == 1)
		{
			tempOther->SetSmashTimer(0.5f);
		}
		else if (m_nLevel == 2)
		{
			tempOther->SetSmashTimer(0.75f);
		}
		else if (m_nLevel == 3)
		{
			tempOther->SetSmashTimer(1);
		}

		DestroyEntityMessage* pMsg = new DestroyEntityMessage(this);
		pMsg->QueueMessage();
	}
	// If you collide with a wall, pull your owner to it
	else if (pOther->GetType() == ENT_WALL && m_bHitEnemy == false)
	{
		m_pOwner->SetSmashVel(m_ptPosition - m_pOwner->GetPosition());

		if (m_nLevel == 1)
			m_pOwner->SetSmashTimer(0.5f);
		else if (m_nLevel == 2)
			m_pOwner->SetSmashTimer(0.75f);
		else if (m_nLevel == 3)
			m_pOwner->SetSmashTimer(1);

		DestroyEntityMessage* pMsg = new DestroyEntityMessage(this);
		pMsg->QueueMessage();
	}
	// If you collide with an enemy player, check if they have the ball
	//		if not, pull them towards you, if they do, attempt to steal it
	else if (pOther->GetType() == ENT_PLAYER && dynamic_cast<const CPlayer*>(pOther)->GetTeam() != dynamic_cast<const CPlayer*>(m_pOwner)->GetTeam())
	{
		m_bHitEnemy = true;

		Entity* tempOther = const_cast<Entity*>(pOther);

		tempOther->SetSmashVel(m_pOwner->GetPosition() - m_ptPosition);

		int stealChance = rand() % 100;

		if (m_nLevel == 1)
		{
			if (dynamic_cast<const CPlayer*>(pOther)->GetCarrying() == true && stealChance <= 30)
			{
				dynamic_cast<CPlayer*>(tempOther)->SetCarrying(false);
				dynamic_cast<CPlayer*>(tempOther)->Throw(tempOther->GetSmashVel());
			}
			else
				tempOther->SetSmashTimer(0.5f);
		}
		else if (m_nLevel == 2)
		{
			if (dynamic_cast<const CPlayer*>(pOther)->GetCarrying() == true && stealChance <= 60)
			{
				dynamic_cast<CPlayer*>(tempOther)->SetCarrying(false);
				dynamic_cast<CPlayer*>(tempOther)->Throw(tempOther->GetSmashVel());
			}
			else
				tempOther->SetSmashTimer(0.75f);
		}
		else if (m_nLevel == 3)
		{
			if (dynamic_cast<const CPlayer*>(pOther)->GetCarrying() == true)
			{
				dynamic_cast<CPlayer*>(tempOther)->SetCarrying(false);
				dynamic_cast<CPlayer*>(tempOther)->Throw(tempOther->GetSmashVel());
			}
			else
				tempOther->SetSmashTimer(1);
		}

		DestroyEntityMessage* pMsg = new DestroyEntityMessage(this);
		pMsg->QueueMessage();
	}
}